package cn.blnp.net.flowable.boot.custom.element;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.IdentityService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * <h3>自定义流程元素案例</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/5 17:32
 */
@Slf4j
@SpringBootTest
public class CusFlowElementTest {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;

    @Test
    public void test() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("flow/cusAttributeProcess.bpmn20.xml")
                .name("自定义流程元素")
                .deploy();
        //设置流程发起人
        identityService.setAuthenticatedUserId("张三");
        //发起流程实例
        ProcessInstance procInst = runtimeService
                .startProcessInstanceByKey("customAttributeProcess");
        log.info("流程实例ID：{}", procInst.getId());
        log.info("流程标题为：{}", procInst.getName());
        identityService.setAuthenticatedUserId(null);
        TaskQuery taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst.getId());
        //查询并办理第一个用户任务
        Task firstTask = taskQuery.singleResult();
        taskService.complete(firstTask.getId());
        //查询并办理第二个用户任务
        Task secondTask = taskQuery.singleResult();
        taskService.complete(secondTask.getId());

        /**
         *  演示结果：
         *  2025-06-06 10:51:50.126  INFO 56532 --- [           main] c.b.n.f.boot.custom.CusFlowElementTest   : 流程标题为：张三发起的请假申请
         * 2025-06-06 10:51:50.199  INFO 56532 --- [           main] c.b.n.f.boot.custom.MyTaskListener       : 发送待办通知：安琪拉，收到一条新的流程待办，流程标题为【张三发起的请假申请】，任务名称为【审批】
         **/
    }
}
